package com.hongsen.cn.service;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Timer;

public class InitService extends Service {
    private static final Logger logger = LoggerFactory.getLogger(InitService.class);

    public static Context context;
    private static Timer timer = new Timer();

    @Override
    public void onCreate() {
        init(getApplication());
        super.onCreate();
    }

    public static void init(Context ctx) {
        //        context = ctx;
        //        //启动守护
        //        final String packageName =PageName();
        //        if (!StringUtils.isEmpty(packageName)) {
        //            timer.schedule(new TimerTask() {
        //                @Override
        //                public void run() {
        //                    if (!isAppOnForeground(packageName)) {
        //                        startAPP(context, packageName);
        //                    }
        //                }
        //            }, 10000, 10000);
        //        }
        //        try {
        //            OutdoorApplication.getInstance().getMzysjSystemManager().ZYSystemBar(0);// 导航栏的隐藏 竖版有的不兼容
        //        }catch ( Exception e){
        //            logger.error("TAG导航栏的隐藏,此机器竖版不兼容");
        //        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    //在进程中去寻找当前APP的信息，判断是否在前台运行
    public static boolean isAppOnForeground(String appPackageName) {
        ActivityManager activityManager =(ActivityManager) context.getApplicationContext().getSystemService(
                Context.ACTIVITY_SERVICE);
        //String packageName =getApplicationContext().getPackageName();
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null)
            return false;
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(appPackageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
        logger.debug(appPackageName + "：不在前端运行");
        return false;
    }

    /*
     * 启动一个app
     */
    public static void startAPP(Context context, String appPackageName) {
        try {
            logger.debug("启动 app：" + appPackageName);
            Intent intent = context.getPackageManager().getLaunchIntentForPackage(appPackageName);
            context.startActivity(intent);
        } catch (Exception e) {
            logger.debug("没有安装 app：" + appPackageName);
        }
    }
    /**
     * 获取包名
     * */
    private static final String PageName() {
        //当前应用pid
        int pid = android.os.Process.myPid();
        //任务管理类
        ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        //遍历所有应用
        List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.pid == pid) {//得到当前应用返回包名
                return info.processName;
            }
        }
        return "";
    }
}
